﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SPCFMedicineInformer.Helpers;
using SPCFMedicineInformer.DocumentTypes;
using umbraco;
using umbraco.cms.businesslogic.web;
using Vega.USiteBuilder;
using umbraco.NodeFactory;
using System.Text;
using umbraco;
namespace SPCFMedicineInformer.Masterpages
{
    public partial class Admin_MedicineGenericNames : System.Web.UI.MasterPage
    {
        public int PageNum { get; set; }
        public int TotalRec { get; set; }
        int limitPerPage = 20;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageNum = 1;
                BindGrid();
                BindDropdown();
            }
        }
        void BindGrid()
        {
            var list = NodeLocator.GetNodesOfExactType<MedicineGenericName>().Where(a => a.Name.ToLower().Contains(txtSearch.Text.ToLower()) &&
                    a.ParentId.ToString().Contains(ddlCategories.SelectedValue));
            list = Sort(list);
            TotalRec = list.Count();
            list = list.Skip((PageNum - 1) * limitPerPage).Take(limitPerPage);
            DisplayPaging();
            rptSymptomsListing.DataSource = list;
            rptSymptomsListing.DataBind();
        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("/admin/utilities/medicine-generic-name/generic-name-entry/");
        }
        void Delete(int id)
        {
            if (id > 0)
            {
                Document doc = new Document(id);
                if (doc.Published)
                {
                    doc.UnPublish();
                    library.UnPublishSingleNode(doc.Id);
                }
                doc.delete();
                BindGrid();
            }
        }
        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case "delete":
                        Delete(intTemp);
                        break;
                    case "paging":
                        PageNum = intTemp;
                        BindGrid();
                        break;
                }
            }
        }
        protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                MedicineGenericName gen = (MedicineGenericName)e.Item.DataItem;
                if (!string.IsNullOrEmpty(gen.Name))
                {
                    Literal litCategory = (Literal)e.Item.FindControl("litCategory");
                    Literal litMedicines = (Literal)e.Item.FindControl("litMedicines");
                    var category = new Node(gen.ParentId);
                    
                    litMedicines.Text = FormHelper.GetChildrenTotal(gen.Id,"MedicineItem");
                    litCategory.Text = category.Name;
                }
            }
        }
        void BindDropdown()
        {
            var catList = NodeLocator.GetNodesOfExactType<MedicineCategory>();
            ddlCategories.DataSource = catList;
            ddlCategories.DataTextField = "Name";
            ddlCategories.DataValueField = "Id";
            ddlCategories.DataBind();

            ddlCategories.Items.Insert(0, new ListItem("Choose...", ""));
            ddlTitles.Items.Add(new ListItem("Id", "Id"));
            ddlTitles.Items.Add(new ListItem("Category", "Category"));
            ddlTitles.Items.Add(new ListItem("Generic Name", "Generic Name"));
        }
        void DisplayPaging()
        {
            int maxPage = (TotalRec / limitPerPage) + (TotalRec % limitPerPage > 0 ? 1 : 0);
            List<string> pageList = new List<string>();
            for (int i = 1; i <= maxPage; i++)
            {
                pageList.Add(i.ToString());
            }

            rptPaging.DataSource = pageList;
            rptPaging.DataBind();

        }
        IEnumerable<MedicineGenericName> Sort(IEnumerable<MedicineGenericName> list)
        {
            if (rdoSort.Text == "Asc")
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderBy(a => a.Id);
                        break;
                    case "Category":
                        list = list.OrderBy(a => a.Parent().Name);
                        break;
                    case "Generic Name":
                        list = list.OrderBy(a => a.Name);
                        break;
                }
            }
            else
            {
                switch (ddlTitles.SelectedValue)
                {
                    case "Id":
                        list = list.OrderByDescending(a => a.Id);
                        break;
                    case "Category":
                        list = list.OrderByDescending(a => a.Parent().Name);
                        break;
                    case "Generic Name":
                        list = list.OrderByDescending(a => a.Name);
                        break;
                }
            }

            return list;
        }
        protected void btnFilter_Click(object sender, EventArgs e)
        {
            PageNum = 1;
            BindGrid();

        }
        protected void Repeater_ItemDatabound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton lb = (LinkButton)e.Item.FindControl("linkButPaging");
                Literal lit = (Literal)e.Item.FindControl("litPaging");
                if (PageNum.ToString() == lit.Text)
                {
                    lb.CssClass = "current";
                }
            }

        }
    }
}